package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblTaiKhoanService;
import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienMonHoc;
import com.lb.jwitter.service.TblThongTinHocVienMonHocService;
import com.lb.jwitter.service.TblThongTinHocVienService;

import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.ExcelToJSon;
import com.lb.jwitter.util.ExcelToJSon2;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblThongTinHocVienMonHocSaveFromXLSAction {


	private TblTaiKhoan taiKhoanDangNhap; 
	  
	  public TblTaiKhoan getTaiKhoanDangNhap() { 
	      return taiKhoanDangNhap; 
	  } 
	  public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
	      this.taiKhoanDangNhap = taiKhoanDangNhap; 
	  } 
	  
	  private int dotDKId;
	  
	public int getDotDKId() {
		return dotDKId;
	}
	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	private Message msg;
	
	  public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}
	
	private JSONArray jsons;

	public JSONArray getJsons() {
		return jsons;
	}
	public void setJsons(JSONArray jsons) {
		this.jsons = jsons;
	}
	
	private String destFile;
	public String getDestFile() {
		return destFile;
	}
	public void setDestFile(String destFile) {
		this.destFile = destFile;
	}
	
	public String execute() throws Exception
	{


	     try{ 	
	    	 
	         Map session = ActionContext.getContext().getSession();
	         taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	         if( taiKhoanDangNhap == null) return "noprivilege";
	         
	        ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 

	        TblThongTinHocVienMonHocService tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService)  context.getBean("tbl_ThongTin_HocVien_MonHocClient"); 
	        TblThongTinHocVienService tblThongTinHocVienService = (TblThongTinHocVienService)context.getBean("tbl_ThongTin_HocVienClient"); 
	        TblCTDTMonHocService tblCTDTMonHocService = (TblCTDTMonHocService)context.getBean("tbl_CTDT_MonHocClient"); 
	        
	        //get json from excel
	        if(ExcelToJSon2.getExtension(destFile).equals("XLSX")){
	        	jsons = ExcelToJSon2.execute(destFile);
	        	System.out.println("EXCEL 2007: " + destFile);
	        }else{
	        	jsons = ExcelToJSon.execute(destFile);
	        	System.out.println("EXCEL 2003: " + destFile);
	        }
	        
	        tblThongTinHocVienMonHocService.createListtblThongTinHocVienMonHocByJSON(jsons.toString());
	       
//	        for(int i = 0; i < jsons.size() ; i++){
//	    		JSONObject obj = jsons.getJSONObject(i);
//	    		
//	    		
//	    		 //new doi tuong thongtinhocvienmonhoc
//		        
//		        //kiem tra mshv co ton tai hay khong
//		        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
//		        {
//		        	 ConditionReport con = new ConditionReport(); 
//		             con.setCol("hocVienId.MSSV");
//		             con.setValue1(obj.getString("MSSV"));
//		             con.setOperator("eq");
//		             cons.add(con);	
//		        }
//		        {
//		        
//			       	 ConditionReport con = new ConditionReport(); 
//			            con.setCol("monHocId.ma");
//			            con.setValue1(obj.getString("MaMonHoc"));
//			            con.setOperator("eq");
//			            cons.add(con);	
//			    }
//		        List<TblThongTinHocVienMonHoc> thongTinHocVienMonHoc = tblThongTinHocVienMonHocService.findtblThongTinHocVienMonHocByCondition(cons, 1, 1);
//		        
//		        //neu ket qua da co=> cap nhat ket qua
//		        if(thongTinHocVienMonHoc != null && thongTinHocVienMonHoc.size() > 0){
//		        	TblThongTinHocVienMonHoc tblThongTinHocVienMonHoc = thongTinHocVienMonHoc.get(0);
//		        	tblThongTinHocVienMonHoc.setTrangThai(obj.getString("TrangThai"));
//		        	
//		        	tblThongTinHocVienMonHocService.updatetblThongTinHocVienMonHoc(tblThongTinHocVienMonHoc);
//		        	
//		        }
//		        //neu chua co ket qua => insert
//		        else{
//		        	TblThongTinHocVienMonHoc tblThongTinHocVienMonHoc = new TblThongTinHocVienMonHoc();
//		        	
//		        	cons.clear();
//			        {
//			        	 ConditionReport con = new ConditionReport(); 
//			             con.setCol("MSSV");
//			             con.setValue1(obj.getString("MSSV"));
//			             con.setOperator("eq");
//			             cons.add(con);	
//			        }
//			        List<TblThongTinHocVien> hocVien = tblThongTinHocVienService.findtblThongTinHocVienByCondition(cons, 1, 1);
//		
//			        cons.clear();
//			        {
//			       	 ConditionReport con = new ConditionReport(); 
//			            con.setCol("ma");
//			            con.setValue1(obj.getString("MaMonHoc"));
//			            con.setOperator("eq");
//			            cons.add(con);	
//			       }
//			       List<TblCTDTMonHoc> monHoc = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 1, 1);
//
//			       //neu ton tai hoc vien co mssv va ton tai mon hoc co ma mon hoc thi insert
//		            if(hocVien != null && hocVien.size() > 0 && monHoc != null && monHoc.size() > 0){
//		            	
//		            	tblThongTinHocVienMonHoc.setHocVienId(hocVien.get(0)); 
//		            	tblThongTinHocVienMonHoc.setMonHocId(monHoc.get(0));
//		            	if(obj.getString("TrangThai").trim().equals("1"))
//		            		tblThongTinHocVienMonHoc.setTrangThai("1");
//		            	else
//		            		tblThongTinHocVienMonHoc.setTrangThai("0");
//		            	
//			    		tblThongTinHocVienMonHocService.createtblThongTinHocVienMonHoc(tblThongTinHocVienMonHoc);
//		            }
//		        }
		        
		        
		      
//	            	//set dotDK
//		    		TblDKMHDotDK tblDKMHDotDK = new TblDKMHDotDK();
//		    		tblDKMHDotDK.setId(new Long(dotDKId));
//		    		tblThongTinMonHoc.setDotDKId(tblDKMHDotDK);
//		    		
//		           //set Can Bo
//		           tblThongTinMonHoc.setCanBoId(taiKhoanDangNhap.getCanBoId());
//		           
//		           //set ngay cap nhat
//		           DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
//		           Date dd = new Date(); 
//		           GregorianCalendar c = new GregorianCalendar(); 
//		           c.setTime(dd);
//		           XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
//		            tblThongTinMonHoc.setNgayCapNhat(d); 
//	    	}
	        
	            return Action.SUCCESS; 
	     } 
	     catch(Exception e){ 
	             System.out.println("Loi add ky hieu from excell Action"); 
	             e.printStackTrace(); 
	             return Action.ERROR; 
	     } 

	}
	
	
	
	
	
}
